package com.linkallcloud.web.session;

import java.util.List;

import com.linkallcloud.core.dto.Sid;
import com.linkallcloud.core.enums.LoginMode;
import com.linkallcloud.core.www.ISessionBUser;

public class SessionBUser extends SessionCUser implements ISessionBUser {
    private static final long serialVersionUID = 1731544740266689535L;

    private boolean admin;// 是否管理员

    private Sid company;// 当前登录的company
    private Sid org;// 当前登录的org

    private Integer loginMode;// 登录模式，normal:普通模式,proxy:代维模式
    private SessionBUser srcUser;

    private Sid area; // 所在区域
    private int areaLevel; // 所在区域level

    private String loginWay;// 登录途径

    // 以下是等到到具体某app相关权限信息
    private Long[] orgPermissions;
    private Long[] areaPermissions;

    private List<Sid> myOrgs;// 所在机构+兼职机构列表

    public SessionBUser() {
        super();
        this.loginMode = LoginMode.Normal.getCode();
    }

    public SessionBUser(Long id, String uuid, String loginName, String name, String userType) {
        this();
        this.setSid(new Sid(id, uuid, loginName, name));
        this.setLoginName(loginName);
        this.setUserType(userType);
    }

    public SessionBUser(Long id, String uuid, String loginName, String name, String userType, Long companyId,
            String companyUuid, String companyCode, String companyName, Long orgId, String orgUuid, String orgCode,
            String orgName) {
        this(id, uuid, loginName, name, userType);
        this.setCompanyInfo(companyId, companyUuid, companyCode, companyName);
        this.setOrgInfo(orgId, orgUuid, orgCode, orgName);
    }

    public void setCompanyInfo(Long id, String uuid, String code, String name) {
        this.company = new Sid(id, uuid, code, name);
    }

    public void setOrgInfo(Long id, String uuid, String code, String name) {
        this.org = new Sid(id, uuid, code, name);
    }

    public void setPermissions(String[] menuPermissions, Long[] orgPermissions, Long[] areaPermissions) {
        this.setMenuPermissions(menuPermissions);
        this.orgPermissions = orgPermissions;
        this.areaPermissions = areaPermissions;
    }

    @Override
    public boolean isAdmin() {
        return admin || this.getLoginName().equals("superadmin");
    }

    @Override
    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    @Override
    public Sid getCompany() {
        return company;
    }

    @Override
    public Long companyId() {
        return company == null ? null : company.getId();
    }

    @Override
    public String companyUuid() {
        return company == null ? null : company.getUuid();
    }

    @Override
    public String companyName() {
        return company == null ? null : company.getName();
    }

    public void setCompany(Sid company) {
        this.company = company;
    }

    @Override
    public Sid getOrg() {
        return org;
    }

    @Override
    public Long orgId() {
        return org == null ? null : org.getId();
    }

    @Override
    public String orgUuid() {
        return org == null ? null : org.getUuid();
    }

    @Override
    public String orgName() {
        return org == null ? null : org.getName();
    }

    public void setOrg(Sid org) {
        this.org = org;
    }

    public Integer getLoginMode() {
        LoginMode mode = LoginMode.get(loginMode);
        if (mode != null) {
            return mode.getCode();
        }
        return LoginMode.Normal.getCode();
    }

    public void setLoginMode(Integer loginMode) {
        this.loginMode = loginMode;
    }

    public SessionBUser getSrcUser() {
        return srcUser;
    }

    public void setSrcUser(SessionBUser srcUser) {
        this.srcUser = srcUser;
    }

    @Override
    public String getLoginWay() {
        return loginWay;
    }

    @Override
    public void setLoginWay(String loginWay) {
        this.loginWay = loginWay;
    }

    @Override
    public Sid getArea() {
        return area;
    }

    @Override
    public Long areaId() {
        return area == null ? null : area.getId();
    }

    @Override
    public String areaUuid() {
        return area == null ? null : area.getUuid();
    }

    @Override
    public String areaCode() {
        return area == null ? null : area.getCode();
    }

    @Override
    public String areaName() {
        return area == null ? null : area.getName();
    }

    public void setArea(Sid area) {
        this.area = area;
    }

    @Override
    public int getAreaLevel() {
        return areaLevel;
    }

    public void setAreaLevel(int areaLevel) {
        this.areaLevel = areaLevel;
    }

    @Override
    public List<Sid> getMyOrgs() {
        return myOrgs;
    }

    public void setMyOrgs(List<Sid> myOrgs) {
        this.myOrgs = myOrgs;
    }

    public Long[] getOrgPermissions() {
        return orgPermissions;
    }

    public void setOrgPermissions(Long[] orgPermissions) {
        this.orgPermissions = orgPermissions;
    }

    public Long[] getAreaPermissions() {
        return areaPermissions;
    }

    public void setAreaPermissions(Long[] areaPermissions) {
        this.areaPermissions = areaPermissions;
    }

    public void setAreaInfo(Long areaId, String uuid, String code, String name, int level) {
        this.area = new Sid(areaId, uuid, code, name);
        this.areaLevel = level;
    }

    public void proxyFrom(SessionBUser srcUser) {
        this.srcUser = srcUser;
        this.loginMode = LoginMode.Proxy.getCode();
    }

}
